package ui;
import Pojo.Account;
import Service.AccountService;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;


public class AccountManager extends JInternalFrame implements ActionListener {
    private AccountService accountService = new AccountService();
    private JTextField txtName ;
    private JPasswordField txtPwd ;
    private DefaultTableModel dtm;
    private JTable table ;
    public AccountManager(){

        super("账户管理",true,true,false,true);
        setBounds(100,100,680,469);

        JScrollPane scrollPane = new JScrollPane();
        getContentPane().add(scrollPane, BorderLayout.CENTER);
        String[] header = {"ID","账户名","密码"};

        dtm = new DefaultTableModel(null,header);
        table = new JTable(dtm){
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        table.setRowHeight(35);
        scrollPane.setViewportView(table);
        JPanel panel = new JPanel();
        panel.setPreferredSize(new Dimension(0,50));
        getContentPane().add(panel,BorderLayout.SOUTH);
        JLabel labelName = new JLabel("账户名");
        panel.add(labelName);
        txtName = new JTextField();
        txtName.setPreferredSize(new Dimension(80,35));
        panel.add(txtName);
        txtName.setColumns(10);
        JLabel labelPwd = new JLabel("密码");
        panel.add(labelPwd);
        txtPwd = new JPasswordField();
        txtPwd.setPreferredSize(new Dimension(80,35));
        panel.add(txtPwd);
        txtPwd.setColumns(10);
        JButton btnUpdate= new JButton("添加账户");
        btnUpdate.addActionListener(this);
        panel.add(btnUpdate);
        JButton btnDel = new JButton("删除");
        btnDel.addActionListener(this);
        panel.add(btnDel);
        loadData();

    }
    public void loadData(){
        dtm.setRowCount(0);
        List<Account> list = accountService.lodingAccountAll();
        String[][] data = accountService.listToArray(list);
        for(int i = 0 ; i < data.length ; i++){
            dtm.addRow(data[i]);
        }
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("添加账户")){
            String name = txtName.getText().trim();
            String pwd = txtPwd.getText().trim();

            accountService.saveAccount(name,pwd);
            txtName.setText("");
            txtPwd.setText("");

            loadData();
        } else if (e.getActionCommand().equals("删除")) {
            int index = table.getSelectedRow();// 获取到当前选中过的行号 -1:没有

            if(index != -1){
                int result = JOptionPane.showConfirmDialog(this,"确定删除？","系统 提示",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
                if(result == JOptionPane.YES_OPTION){

                    accountService.delAccount(index);
                    JOptionPane.showMessageDialog(this,"删除成功！");
                    loadData();
                }
            }
        }
    }
}